package vip.liux.front.adapter.web.account;

import jakarta.servlet.ServletException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.liux.contracts.security.SignManager;

import java.io.IOException;

/**
 * 账户通行证控制器
 */
@RestController
@RequestMapping("/api/account/passport")
public class PassportController {

    private static final Logger log = LoggerFactory.getLogger(PassportController.class);
    private final SignManager signManager;

    public PassportController(SignManager signManager) {
        this.signManager = signManager;
    }

    /**
     * 用户登录 (手机号+验证码)
     */
    @PreAuthorize("hasRole('ROLE_USER')")
    @PostMapping("/login")
    public void login(@Valid @RequestBody LoginViewModel vm) throws ServletException, IOException {
        log.info("login: {}", vm);
        signManager.authentication(new UsernamePasswordAuthenticationToken(vm.mobile(), vm.code()));
    }

    public record LoginViewModel(@NotBlank String mobile, @NotBlank String code) {

    }
}
